package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import entity.Project;
import util.DBHelp;
import util.RowMapper;

public class ProjectDao {
		private DBHelp<Project> db=new DBHelp<Project>();
		
		public void save(Project project){
			String sql="INSERT INTO t_project(id,p_name,description) VALUES(?,?,?)";
			db.executeSQL(sql,project.getId(),project.getP_name(),project.getDescription());
		}
		
		public Project findProjectById(String id){
			String sql="SELECT id ,p_name,description,createtime FROM t_project WHERE id=?";
			return db.executeQueryToObject(sql,new ProjectRowMapper(), id);
			
		}
		
		public class ProjectRowMapper implements RowMapper<Project>{

			public Project mapRow(ResultSet rs) throws SQLException {
				Project project=new Project();
				
				project.setId(rs.getString("id"));
				project.setP_name(rs.getString("p_name"));
				project.setDescription(rs.getString("description"));
				project.setCreatetime(rs.getString("createtime"));
				
				
				return project;
			}
			
						
		}

		public List<Project> findAllProject() {
			String sql="SELECT id, p_name,description,createtime FROM t_project order by createtime desc";
			return db.executeQueryToList(sql,new ProjectRowMapper());
		} 
		public void update(Project pro){
			String sql="update t_project set p_name=?,description=? where id=?";
			db.executeSQL(sql, pro.getP_name(),pro.getDescription(),pro.getId());
		}
}
